package com.ruoyi.common;

import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.MappedTypes;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;


   public class StringArrayTypeHandler extends BaseTypeHandler<String[]> {

       @Override
       public void setNonNullParameter(PreparedStatement ps, int i, String[] parameter, JdbcType jdbcType) throws SQLException {
           StringBuilder sb = new StringBuilder();
           for (String str : parameter) {
               if (sb.length() > 0) {
                   sb.append(",");
               }
               sb.append(str);
           }
           ps.setString(i, sb.toString());
       }

       @Override
       public String[] getNullableResult(ResultSet rs, String columnName) throws SQLException {
           String value = rs.getString(columnName);
           if (value == null) {
               return null;
           }
           return value.split(",");
       }

    @Override
    public String[] getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
           String value = rs.getString(columnIndex);
        if (value == null) {
            return null;
        }
        return value.split(",");
    }

    @Override
       public String[] getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
           String value = cs.getString(columnIndex);
           if (value == null) {
               return null;
           }
           return value.split(",");
       }
   }
   